$OpenBSD: patch-liba52_bitstream_c,v 1.5 2012/09/04 06:17:51 ajacoutot Exp $

- Correct type for pointers arithmetic manipulation.
- Fix random crashes caused by invalid 32-bit shifts on 32-bit values.

--- liba52/bitstream.c.orig	Mon Sep  3 19:03:40 2012
+++ liba52/bitstream.c	Mon Sep  3 19:06:39 2012
@@ -23,6 +23,7 @@
 
 #include "config.h"
 
+#include <stddef.h>
 #include <inttypes.h>
 
 #include "a52.h"
@@ -33,9 +34,9 @@
 
 void a52_bitstream_set_ptr (a52_state_t * state, uint8_t * buf)
 {
-    int align;
+    ptrdiff_t align;
 
-    align = (long)buf & 3;
+    align = (ptrdiff_t)buf & 3;
     state->buffer_start = (uint32_t *) (buf - align);
     state->bits_left = 0;
     state->current_word = 0;
@@ -62,11 +63,14 @@ static inline void bitstream_fill_current (a52_state_t
 
 uint32_t a52_bitstream_get_bh (a52_state_t * state, uint32_t num_bits)
 {
-    uint32_t result;
+    uint32_t result = 0;
 
-    num_bits -= state->bits_left;
-    result = ((state->current_word << (32 - state->bits_left)) >>
-	      (32 - state->bits_left));
+    if (state->bits_left)
+    {
+	num_bits -= state->bits_left;
+	result = ((state->current_word << (32 - state->bits_left)) >>
+		  (32 - state->bits_left));
+    }
 
     bitstream_fill_current (state);
 
@@ -80,11 +84,14 @@ uint32_t a52_bitstream_get_bh (a52_state_t * state, ui
 
 int32_t a52_bitstream_get_bh_2 (a52_state_t * state, uint32_t num_bits)
 {
-    int32_t result;
+    int32_t result = 0;
 
-    num_bits -= state->bits_left;
-    result = ((((int32_t)state->current_word) << (32 - state->bits_left)) >>
-	      (32 - state->bits_left));
+    if (state->bits_left)
+    {
+	num_bits -= state->bits_left;
+	result = ((((int32_t)state->current_word) << (32 - state->bits_left))
+		  >> (32 - state->bits_left));
+    }
 
     bitstream_fill_current(state);
 
